package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mappers.SpecMapper;
import com.leyou.item.mappers.SpecParamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xiang
 * @Date: 2019/8/18 12:09
 * @Description:
 */
@Service
@Transactional
@Slf4j
public class SpecService {
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroupDTO> specGroup(Long id) {
        try {
            SpecGroup specGroup = new SpecGroup();
            specGroup.setCid(id);

            List list = specMapper.select(specGroup);
            List<SpecGroupDTO> specGroupList = BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
            return specGroupList;
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

    }

    public List<SpecParamDTO> querySpceParam(Long gid, Long cid, Boolean searching) {
        //gid,和cid必须传一个
        if (gid == null && cid == null && null == searching) {
            throw new LyException(ExceptionEnum.INVALID_NOTIFY_PARAM);

        }


        try {
            SpecParam specParam = new SpecParam();
            specParam.setGroupId(gid);
            specParam.setCid(cid);
            specParam.setSearching(searching);
            List<SpecParam> specParamList = specParamMapper.select(specParam);
            List<SpecParamDTO> specParamDTOS = BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
            return specParamDTOS;
        } catch (Exception e) {
            log.debug(e.getMessage());
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
    }

    public List<SpecGroupDTO> querySpecsByCid(Long cid) {
        //分组先查出来然后通过分组查到规格参数
        List<SpecGroupDTO> specGroupDTOS = specGroup(cid);
        List<SpecParamDTO> specParamDTOS = querySpceParam(null, cid, null);
        //笨笨的写法 ，双重for循环，性能略差
       /* for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            List<SpecParamDTO> specParamDTOList = new ArrayList<>();
            for (SpecParamDTO specParamDTO : specParamDTOS) {
                if (specGroupDTO.getId() == specParamDTO.getGroupId()) {
                    specParamDTOList.add(specParamDTO);

                }
                specGroupDTO.setParams(specParamDTOList);
            }

        }*/
       //开启牛逼写法 // 将规格参数按照groupId进行分组，得到每个group下的param的集合
        Map<Long, List<SpecParamDTO>> paramMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //然后添加进specGroupDTOs
        for (SpecGroupDTO groupDTO : specGroupDTOS) {
            groupDTO.setParams(paramMap.get(groupDTO.getId()));

        }
        return specGroupDTOS;

    }
}